package com.weasing.res.utils;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.weasing.res.domain.WxAdmin;

/**
 * 项目公用工具类
 * 
 * @author long
 *
 */
public class RESUtils {
	
	/**
	 * 获取session
	 * 
	 * @return
	 */
	public static HttpSession getSession(){
		HttpServletRequest request = ServletActionContext.getRequest();
		return request.getSession();
	}

	/**
	 * 获取request
	 * 
	 * @return
	 */
	public static HttpServletRequest getRequest() {
		HttpServletRequest request = ServletActionContext.getRequest();
		return request;
	}

	
	/**
	 * 获取用户-res系统
	 * 
	 * @return
	 */
	public static WxAdmin getLoginWxAdmin() {
		return (WxAdmin) ServletActionContext.getRequest().getSession()
				.getAttribute("wxAdmin");
	}

	/**
	 * 异步处理数据
	 * 
	 * @param String
	 *            ---json
	 */
	public static void print(String json) {
		try {
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 异步处理数据
	 * 
	 * @param String
	 *            ---json
	 */
	public static void printJSON(Object json) {
		try {
			ServletActionContext.getResponse().setContentType(
					"text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 获得response
	 * 
	 * @author 黄龙
	 * @return
	 */
	public static HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
}
